
#include "opencvhelper.h"

#include "stdlib.h"
#include <random>
#include <QTime>
using namespace cv;
using namespace std;


QHash<QString,ProcessFun *> processFunMap;
void addProcessFunMap(QString name,processFun_t fun, QString description,  string defaultArg)
{
    auto tmp =new ProcessFun(name,
                              description,
                              fun,
                              defaultArg);
    if(processFunMap.contains(name)){criticalX(name);}
    processFunMap.insert(name,tmp);
}
inline void addProcessFunMap(QString name, QString description, processFun_t fun, string defaultArg)
{
    addProcessFunMap(name,fun, description,defaultArg);
}



void initProcessFunMap()
{
void cannyDemo(Mat &,Mat &,Mat &,string );
addProcessFunMap("edge-canny","Canny边缘检测edge detection ",cannyDemo,"200");

void colorInverting(Mat &,Mat &,Mat &,string );
addProcessFunMap("colorInverting","反色图",colorInverting,"");

void equalizeHistDemo(Mat &,Mat &,Mat &,string );
addProcessFunMap("equalizeHist",equalizeHistDemo,"直方图均衡化","");


}

using namespace cv;
using namespace std;



//通过均衡化处理使得输入图像的直方图变得范围较广以及分布较均匀
void equalizeHistDemo(Mat &input,Mat &output,Mat &,string)
{
    int channels =input.channels();
    switch (channels) {
    case 1:
        equalizeHist(input,output);
        return;
    case 2:
    case 3:
    {
        Mat imageRGB[channels];
        split(input, imageRGB);
        for (int i = 0; i < channels; i++)
        {
            equalizeHist(imageRGB[i], imageRGB[i]); //只支持单通道 CV_8UC1
        }
        Mat result;
        merge(imageRGB, channels, result);
        output=result;
    }
        return;
    default:
        criticalX("channels:"<<channels);
        output=input;
        break;
    }

}


//反色图：将图像亮暗对调，可以增强图像中暗色区域细节
void colorInverting(Mat &input,Mat &output,Mat &,string)
{

    int channels =input.channels();
    output=input;
    switch (channels) {
    case 1:
        for (int i = 0; i < input.rows; i++) {
            for (int j = 0; j < input.cols; j++) {

                output.at<uchar>(i, j) = 255 - input.at<uchar>(i, j); // 灰度反转
            }
        }
        return;
    case 2:
    case 3:
    {

        for (int i = 0; i < input.rows; i++)
        {
            for (int j = 0; j < input.cols; j++)
            {
                for (int k = 0; k < channels; k++)
                {
                    output.at<Vec3b>(i, j)[k] = 255 - input.at<Vec3b>(i, j)[k];  //反转
                }
            }
        }
    }
        return;
    default:
        criticalX("channels:"<<channels);

        break;
    }

}


void cannyDemo(Mat &input,Mat &output,Mat &,string arg)
{
    debugx2(arg);
    auto value=atoi(arg.c_str());
    if(input.channels()==3)cvtColor(input, input,COLOR_BGR2GRAY);
    Canny(input, output, value, value*2, 3, false);
}



